<template>
	<view class="container">
		<!-- 无设备提示 -->
		<view v-if="noDeviceFound">
			<up-modal :show="noDeviceFound" title="无此设备，请检查设备编号或重新扫码。" @confirm="confirm"></up-modal>
		</view>

		<view class="body">
			<!-- 搜索条 -->
			<up-sticky bgColor="#fff">
				<view class="search">
					<up-search placeholder="输入设备编号" v-model="sbbhSearchWord" @search="sbbhSearch" @custom="sbbhSearch"
						@clickIcon="scanQRCode" searchIcon="scan" scrollable="false" :show-action="false" shape="round"
						height="36" bgColor="#fff" placeholderColor="#999" searchIconColor="#252B93" color="#333" />
				</view>
			</up-sticky>

			<!-- 设备列表（分页加载） -->
			<view class="task-list">
				<view class="task-item" @tap="dataInfo(item)" v-for="(item) in ListData" :key="item.id">
					<view class="task-info">
						<view class="device-name">{{ item.sbmc }}</view>
						<view class="device-code">设备编号：{{ item.sbbh }}</view>
						<view class="location">使用部门：{{ item.bmbh_id.bmbh_id_name }}</view>
					</view>
				</view>
			</view>

			<!-- 加载更多提示 -->

			<view class="load-more" v-if="isLoading">
				<u-loadmore status="loading" />
			</view>
			<view class="load-more" v-else-if="isFinished && ListData.length > 0">
				<u-loadmore status="nomore" />
			</view>
			<view class="empty-tips" v-else-if="!isLoading && ListData.length === 0">
				<u-empty mode="data" text="暂无数据">
				</u-empty>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sbbhSearchWord: '',
				ListData: [],
				noDeviceFound: false,
				isLoading: false, // 加载状态
				isFinished: false, // 是否加载完成
				entityFilter: {
					FilterConditions: [], //筛选
					SortConditions: [], //排序
					IsUsePagination: true, //是否分页
					Pagination: { //分页相关参数
						PageIndex: 1,
						PageSize: 20,
						PageCount: 0,
						TotalCount: 0
					}
				}
			};
		},
		onLoad() {
			this.getDeviceData();
		},
		onShow() {
			this.getDeviceData();
		},

		// 滚动加载下一页
		onReachBottom() {
			if (this.isLoading || this.isFinished) return;
			this.loadMore();
		},
		methods: {
			// 重置参数并刷新数据
			resetAndRefresh() {
				this.entityFilter.Pagination.PageIndex = 1;
				this.ListData = [];
				this.isFinished = false;
				this.getDeviceData();
			},
			// 搜索设备并弹窗
			async sbbhSearch() {
				this.noDeviceFound = false;
				this.resetAndRefresh();
			},

			async getDeviceData() {
				if (this.isLoading) return;
				this.isLoading = true;

				try {

					// 设置筛选条件
					this.entityFilter.FilterConditions = this.sbbhSearchWord.trim() ? [{
						FilterField: "sbbh",
						Compare: 6, // 包含
						Value: this.sbbhSearchWord,
						Expresstype: 0,
						relation: 0
					}] : [];

					const res = await this.$commonUnits.request(
						'sbssdl/sbssgl/v1.0/sbtzout', 'GET', {
							"entityFilter": this.entityFilter
						}, "48ddc0b3-cbc4-2f33-004f-6c02cdb00c83"
					);

					if (res.statusCode == 200) {

						var result = res.data.result || []

						this.entityFilter.Pagination.PageCount = res.data.pagination.pageCount
						this.entityFilter.Pagination.TotalCount = res.data.pagination.totalCount

						// 第一页直接赋值，后续页追加
						if (this.entityFilter.Pagination.PageIndex === 1) {
							this.ListData = result;
						} else {
							this.ListData = [...this.ListData, ...result];
						}

						// 判断是否已加载全部数据
						this.isFinished = result.length < this.entityFilter.Pagination.PageSize ||
							this.ListData.length >= this.entityFilter.Pagination.TotalCount;

						// 无数据提示
						if (this.entityFilter.Pagination.PageIndex === 1 && result.length === 0) {
							this.noDeviceFound = true;
						}

					} else {
						uni.showToast({
							title: '数据加载失败，请重试',
							icon: 'none'
						});
					}
				} catch (err) {
					console.log(err)
					uni.showToast({
						title: '数据加载失败',
						icon: 'none'
					});
				} finally {
					this.isLoading = false;
				}
			},

			// 加载更多数据
			loadMore() {
				if (this.isFinished) return;
				this.entityFilter.Pagination.PageIndex++;
				this.getDeviceData();
			},

			// 扫码功能
			scanQRCode() {
				uni.scanCode({
					success: (res) => {
						this.sbbhSearchWord = res.result;
						this.sbbhSearch();
					},
					fail: () => {
						uni.showToast({
							title: '扫码失败',
							icon: 'none'
						});
					}
				});
			},

			// 点击设备列表项跳转
			dataInfo(item) {
				try {
					const itemJson = JSON.stringify(item);
					const encodedItem = encodeURIComponent(itemJson);
					uni.navigateTo({
						url: `/pageTwo/pages/record/detail?item=${encodedItem}`
					});
				} catch (error) {
					console.error('对象转换失败:', error);
					uni.showToast({
						title: '数据传递失败',
						icon: 'none'
					});
				}
			},
			confirm() {
				this.noDeviceFound = false;
			},
		}
	};
</script>
<style scoped lang="scss">
	.body {
		height: 100%;
		padding-bottom: 20rpx;
	}

	.search {
		padding-top: 10px;
		background-color: #fff;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
		transition: all 0.3s ease;

		&:active {
			transform: translateY(2rpx);
		}

		:deep(.u-search__content) {
			border-radius: 50rpx !important;
			border: 2rpx solid #1890ff !important;
			box-shadow: 0 2rpx 8rpx rgba(37, 43, 147, 0.1);
			border: 1rpx solid #eee;

			&:focus-within {
				border-color: #1890ff !important;
				box-shadow: 0 2rpx 12rpx rgba(37, 43, 147, 0.2);
			}
		}

		:deep(.u-search__content__input) {
			font-size: 14px;
			padding-left: 20rpx;
		}

		:deep(.u-icon__icon) {
			font-size: 18px !important;
		}
	}

	.task-list {
		padding: 10rpx 20rpx;
	}

	.task-item {
		background: #ffffff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.device-name {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 12rpx;
	}

	.device-code,
	.location {
		min-width: 160rpx;
		font-size: 28rpx;
		color: #666;
		margin-bottom: 8rpx;
	}

	.task-status {
		flex: 1;
	}

	.check-pass {
		text-align: right;
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 12rpx;
	}

	.check-error {
		text-align: right;
		font-size: 32rpx;
		font-weight: bold;
		color: #ff0000;
		margin-bottom: 12rpx;
	}

	.task-button {
		display: flex;
		align-items: center;
		padding: 8rpx 16rpx;
		background: #f6ffed;
		border-radius: 4rpx;
		border: 1rpx solid #52c41a;
		cursor: pointer;
	}

	.action-text {
		font-size: 24rpx;
		color: #52c41a;
	}
</style>